<?php

namespace App\Jobs\RemoveDelCatalogData;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;


class CopyTable implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $idsInfo            = $this->getPartAndProjectClassifyIds();
        $partCompanyIds     = $idsInfo['part_company_ids'];
        $projectClassifyIds = $idsInfo['project_classify_ids'];

        foreach ($partCompanyIds as $partCompanyId) {
            foreach ($projectClassifyIds as $projectClassifyId) {
                $table1 = "book_catalogs_{$partCompanyId}_{$projectClassifyId}";
                if (Schema::hasTable($table1)) {
                    if (DB::table($table1)->whereNotNull('deleted_at')->value('id')) { //如果有删除的数据
                        /*** 开始 */
                        $table1    = "book_catalogs_{$partCompanyId}_{$projectClassifyId}";
                        $table2    = "book_catalog_pdfs_{$partCompanyId}_{$projectClassifyId}";
                        $tableCopy = "book_catalogs_{$partCompanyId}_{$projectClassifyId}_copy";
                        DB::update("create table if not exists {$tableCopy}   like book_catalogs");

                        $copyLogData = [ //定义写入的数据
                            'part_company_id'     => $partCompanyId,
                            'project_classify_id' => $projectClassifyId,
                            'table_name1'         => $table1,
                            'table_name2'         => $tableCopy,
                            'created_at'          => date('Y-m-d H:i:s'),
                        ];

                        if (Schema::hasTable($table2)) { //如果有了pdf
                            $copyLogData['is_have_pdf'] = 1;
                            if (!Schema::hasColumn($tableCopy, 'pre_id')) {
                                Schema::table($tableCopy, function (Blueprint $table) {
                                    $table->integer('pre_id');
                                });
                            }
                            DB::table($table1)
                                ->whereNull('deleted_at')
                                ->orderBy('book_title_id')
                                ->orderBy('serial_no')
                                ->chunk(1000, function ($list) use ($tableCopy) {
                                    foreach ($list as $key2 => $val2) {
                                        $batchArr[$key2]['pre_id']        = $val2->id;
                                        $batchArr[$key2]['serial_no']     = $val2->serial_no;
                                        $batchArr[$key2]['book_title_id'] = $val2->book_title_id;
                                        $batchArr[$key2]['title']         = $val2->title;
                                        $batchArr[$key2]['file_number']   = $val2->file_number;
                                        $batchArr[$key2]['director']      = $val2->director;
                                        $batchArr[$key2]['c_date']        = $val2->c_date;
                                        $batchArr[$key2]['page_num']      = $val2->page_num;
                                        $batchArr[$key2]['is_have_pdf']   = isset($val2->file_url) ?? 0;
                                        $batchArr[$key2]['is_pic']        = $val2->is_pic;
                                        $batchArr[$key2]['problem']       = $val2->problem;
                                        $batchArr[$key2]['creator_id']    = $val2->creator_id;
                                        $batchArr[$key2]['remark']        = $val2->remark;
                                        $batchArr[$key2]['created_at']    = $val2->created_at;
                                        $batchArr[$key2]['updated_at']    = $val2->updated_at;
                                    }
                                    if (!empty($batchArr)) {
                                        DB::table($tableCopy)->insert($batchArr);
                                    }
                                });
                        } else {
                            $copyLogData['is_have_pdf'] = 0;
                            DB::table($table1)
                                ->whereNull('deleted_at')
                                ->orderBy('book_title_id')
                                ->orderBy('serial_no')
                                ->chunk(1000, function ($list) use ($tableCopy) {
                                    foreach ($list as $key2 => $val2) {
                                        $batchArr[$key2]['serial_no']     = $val2->serial_no;
                                        $batchArr[$key2]['book_title_id'] = $val2->book_title_id;
                                        $batchArr[$key2]['title']         = $val2->title;
                                        $batchArr[$key2]['file_number']   = $val2->file_number;
                                        $batchArr[$key2]['director']      = $val2->director;
                                        $batchArr[$key2]['c_date']        = $val2->c_date;
                                        $batchArr[$key2]['page_num']      = $val2->page_num;
                                        $batchArr[$key2]['is_have_pdf']   = isset($val2->file_url) ?? 0;
                                        $batchArr[$key2]['is_pic']        = $val2->is_pic;
                                        $batchArr[$key2]['problem']       = $val2->problem;
                                        $batchArr[$key2]['creator_id']    = $val2->creator_id;
                                        $batchArr[$key2]['remark']        = $val2->remark;
                                        $batchArr[$key2]['created_at']    = $val2->created_at;
                                        $batchArr[$key2]['updated_at']    = $val2->updated_at;
                                    }
                                    if (!empty($batchArr)) {
                                        DB::table($tableCopy)->insert($batchArr);
                                    }
                                });
                        }
                        DB::table('copy_table_logs')->insert($copyLogData);
                        /***结束 */
                    }
                }
            }
        }
    }

}
